﻿using Business.Base.DB.Query;
using Business.Base.Model;
using Business.Base.Service;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Pumptester.ExperimentList
{
    public delegate void LoadDataGrid(DataGridView dataGrid, List<object> list);
    public class ExperimentListQueryAction
    {
        DataGridView dataGrid;
        ExperimentQuery  query;
        int page;

        private ExperimentService experimentService;
        public static int PageSize = 10;
        int maxPage = 0 ;

       
        public LoadDataGrid loadDataGrid;


        public ExperimentQuery Query
        {
            get => query;
            set => query = value;
        }

        public int Page
        {
            get => page;
            set => page = value;
        }

        public ExperimentListQueryAction(ExperimentService experimentService,DataGridView dataGrid)
        {
            this.experimentService = experimentService;
            this.dataGrid = dataGrid;
        }

        public int CalcCurrentRecordCount()
        {
            return  this.experimentService.GetExperimentCount(this.Query);
            
        }

        public int CalcMaxPageNum()
        {
            int recordCount = this.experimentService.GetExperimentCount(this.Query);
            return recordCount / PageSize == 0 ? recordCount / PageSize : recordCount / PageSize + 1;
        }

        public void LoadData()
        {
            List<object> list = experimentService.QueryExperiment(this.Query);  
            this.loadDataGrid(this.dataGrid, list);
        }

        public List<object> LoadRecord(string experimentCode)
        {
            ExperimentRecordQuery recordQuery = new ExperimentRecordQuery();
            recordQuery.PageNum =-1;
            //recordQuery.PageSize = 100;
            recordQuery.ExperimentCode = experimentCode;
            List<object> list  = experimentService.QueryExperimentRecord(recordQuery);
            return list;
        }

        public BaseChartData loadChart(Experiment experiment)
        {
            return experimentService.CaculateChartData(experiment);
        }
    }
}
